using System;
using System.Collections.Generic;
using System.Linq;
using data.contracts;
using modelbuilder.parser.pins;
using modelbuilder.parser.type;

namespace generator
{
    public static class BoardSyntax
    {
        public enum Direction
        {
            Input,
            Output
        }

        public static string Pin(Pin pin, Direction direction) {
            var name = pin.Name;
            if (string.IsNullOrWhiteSpace(name)) {
                name = (direction == Direction.Input) ? "Process" : "Result";
            }
            return name;
        }

        public static string ParsedType(string type) {
            if (String.IsNullOrWhiteSpace(type)) {
                return type;
            }
            return new EbcTypeParser().Parse(type);
        }

        public static IEnumerable<Pin> Pins(IEnumerable<Wire> wires) {
            return wires
                .Select(x => ParsedPin(x.From))
                .Union(wires.Select(x => ParsedPin(x.To)))
                .Distinct(new PinBoardEqualityComparer());
        }

        public static Pin ParsedPin(string text) {
            return new EbcPinParser().Parse(text).First();
        }

        private class PinBoardEqualityComparer : IEqualityComparer<Pin>
        {
            public bool Equals(Pin x, Pin y) {
                return x.Board == y.Board;
            }

            public int GetHashCode(Pin obj) {
                return obj.Board.GetHashCode();
            }
        }
    }
}